package com.example.getroot;

import android.content.Context;
import android.util.Log;

import com.cm.root.SuExec;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

/**
 * Created by suj on 16-8-19.
 */
public class RootChecker {
    private final static String TAG = "RootChecker";

    public static void checkIfRooted(Context context) {
        Log.e(TAG, "start to check ");
        try {
            Process su = Runtime.getRuntime().exec("su");
            DataOutputStream dataOutputStream = new DataOutputStream(su.getOutputStream());
            dataOutputStream.writeBytes("exit\n");
            dataOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        checkDataDir(context);


    }

    /**
     * 这个方法是可以用的，这个检查是不是本地已经root了，
     */


    public static void checkByExec() {
        boolean mobileRoot = SuExec.getInstance().isMobileRoot();
        Log.e(TAG, "moblie is rooted" + mobileRoot);

    }


    public static void checkDataDir(Context context) {
        String data = "data/data";
        File file = new File(data);
        Log.e(TAG, file.exists() + "--");
        Log.e(TAG, file.getAbsolutePath() + "--");
        Log.e(TAG, context.getFilesDir().getAbsolutePath());
        try {
            Log.e(TAG, context.getFilesDir().getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                Log.e(TAG, file1.getAbsolutePath());
            }
        } else {
            Log.e(TAG, "non");

        }

    }
}
